package com.maomao.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/4/13.
 */
@Controller
@RequestMapping("/test")
// /test/**
public class TestController {
    // /test/view post 提交数据，模拟表单
    @RequestMapping(value = "/view", method = RequestMethod.POST)
    public void viewTest(PrintWriter out, HttpServletResponse response, @RequestParam("param1") String param1,
                         @RequestParam("param2") String param2) {
        response.setContentType("application/json;charset=UTF-8");
        Gson gson = new GsonBuilder().create();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("param1", param1);
        map.put("param2", param2);
        System.out.println(gson.toJson(map));
        out.print(gson.toJson(map));
    }

    // /test/view?param1=aaa&param2=bbb get
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public void viewTest3(PrintWriter out, HttpServletResponse response, @RequestParam("param1") String param1,
                          @RequestParam("param2") String param2) {
        response.setContentType("application/json;charset=UTF-8");
        Gson gson = new GsonBuilder().create();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("param1", param1);
        map.put("param2", param2);
        System.out.println(gson.toJson(map));
        out.print(gson.toJson(map));
    }

    // /test/view2/{courseId}
    @RequestMapping(value = "/view2/{param}", method = RequestMethod.GET)
    public void viewTest1(PrintWriter out, HttpServletResponse response, @PathVariable("param") String param) {
        response.setContentType("application/json;charset=UTF-8");
        Gson gson = new GsonBuilder().create();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("param", param);
        out.print(gson.toJson(map));
    }

    // /test/view3
    @RequestMapping(value = "/view3", method = RequestMethod.POST)
    public void viewTest2(PrintWriter out, HttpServletResponse response) {
        response.setContentType("application/json;charset=UTF-8");
        Gson gson = new GsonBuilder().create();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", "success");
        out.print(gson.toJson(map));
    }


}
